﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;
using System.Text.RegularExpressions;
using QLTT.Properties;

namespace QLTT.ViewModels
{
    class StudentClassViewModel:WorkspaceViewModel,System.ComponentModel.IDataErrorInfo
    {
        private StudentClass _studentClass;
        Regex rxValidate;
        private bool _isSelected;
        private icanDataContext _dataContext;
        private Class _class;
        private Student _student;
        private double _fee;
        private bool _status;
        private System.Windows.Visibility _changeClassVisibility;
        private DelegateCommand _changeClassCommand;
        public StudentClassViewModel(icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _studentClass = new StudentClass();
            _class = _dataContext.Classes.Where(o => o.ID == _studentClass.Class).Select(o => o).FirstOrDefault();
            _student = _dataContext.Students.FirstOrDefault(s => s.ID == _studentClass.Student);
        }
        public StudentClassViewModel(icanDataContext dataContext, StudentClass studentClass)
        {
            _studentClass = studentClass;
            _dataContext = dataContext;
            _class = _dataContext.Classes.Where(o => o.ID == _studentClass.Class).Select(o => o).FirstOrDefault();
            _student = _dataContext.Students.FirstOrDefault(s => s.ID == _studentClass.Student);
            FeePayment oFeePayment = _dataContext.FeePayments.Where(o => o.Class == studentClass.Class).Where(o => o.Student == studentClass.Student).Select(o => o).FirstOrDefault();
            
            _status = _studentClass.Status == 0 ? false : true;
            if (!_status && (oFeePayment == null)) _changeClassVisibility = System.Windows.Visibility.Visible;
            else _changeClassVisibility = System.Windows.Visibility.Hidden;
            if (_class.EndDate <= DateTime.Now || _class.StartDate > DateTime.Now)
                this.IsEnableStatus = false;
            else this.IsEnableStatus = true;
        }
        public ICommand ChangeClassCommand
        {
            get
            {
                if (_changeClassCommand == null)
                    _changeClassCommand =new DelegateCommand(ChangeClass);
                return _changeClassCommand;
            }
        }
        public long ID
        {
            get { return _studentClass.ID; }
        }

        public long Student
        {
            get { return _studentClass.Student; }
            set
            {
                if (_studentClass.Student == value) return;
                _studentClass.Student = value;
                base.OnPropertyChanged("Student");
            }
        }
        public long Class
        {
            get { return _studentClass.Class; }
            set
            {
                if (_studentClass.Class == value) return;
                _studentClass.Class = value;
                base.OnPropertyChanged("Class");
            }
        }
        public string StudentName
        {
            get
            {
                return _student.Name;
            }
        }
        public Nullable<double> MidTermMark
        {
            get { return _studentClass.MidTermMark; }
            set
            {
                if (_studentClass.MidTermMark == value) return;
                _studentClass.MidTermMark = value;
                base.OnPropertyChanged("MidTermMark");
            }
        }
        public Nullable<double> FinalMark
        {
            get { return _studentClass.FinalMark; }
            set
            {
                if (_studentClass.FinalMark == value) return;
                _studentClass.FinalMark = value;
                base.OnPropertyChanged("FinalMark");
            }
        }
        public bool Status
        {
            get { return _status; }
            set
            {
                if (_status == value) return;
                _status = value;
                if (_status)
                {
                    _studentClass.Status = 1;
                    this.ChangeClassVisibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    _studentClass.Status = 0;
                    this.ChangeClassVisibility = System.Windows.Visibility.Visible;
                }
                _dataContext.SubmitChanges();
                base.OnPropertyChanged("ChangeClassVisibility");
                base.OnPropertyChanged("Status");
            }
        }
        public bool IsEnableStatus
        {
            get;
            set;
        }
        public double Fee
        {
            get { return _studentClass.Fee; }
            set
            {
                if (_studentClass.Fee == value) return;
                _studentClass.Fee = value;
                base.OnPropertyChanged("Fee");
            }
        }
        public string ClassName
        {
            get {
                return _class.Name;
            }
        }
        public DateTime StartDate
        {
            get
            {
                return _class.StartDate;
            }
        }
        public DateTime EndDate
        {
            get
            {
                return _class.EndDate;
            }
        }
        
        public DateTime LastUpdated
        {
            get { return _studentClass.LastUpdated.Value; }
            set
            {
                if (_studentClass.LastUpdated.Value == value) return;
                _studentClass.LastUpdated = value;
                base.OnPropertyChanged("LastUpdated");
            }
        }
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged("IsSelected");
            }
        }
        public void Save()
        {
            try
            {
                if (_dataContext == null || _studentClass == null)
                    return;
                bool isInsert = _studentClass.ID == 0;
                if (isInsert)
                {
                    _dataContext.StudentClasses.InsertOnSubmit(_studentClass);
                }
                _dataContext.SubmitChanges();
                
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }

        }
        public System.Windows.Visibility ChangeClassVisibility {
            get { return _changeClassVisibility; }
            set 
            {
                if (_changeClassVisibility == value) return;
                _changeClassVisibility = value;
                base.OnPropertyChanged("ChangeClassVisibility");
            } 
        }
        private void ChangeClass()
        {
            ClassScheduleViewModel cSvm = new ClassScheduleViewModel(_dataContext, true,this);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, cSvm);
        }
        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string property]
        {
            get
            {
                string msg = null;
                switch (property)
                {
                    case "FinalMark":
                        if (this.FinalMark != null && this.FinalMark< 0)
                            msg = Resources.ValidationStudentClassFinalMark_Error;
                        break;
                    case "MidTermMark":
                        if (this.MidTermMark != null && this.MidTermMark < 0)
                            msg = Resources.ValidationStudentClassMidTermMark_Error;
                        break;
                    default:
                        throw new ArgumentException(
                            Resources.ValidationDefaultException_Error + property);
                }
                return msg;
            }
        }

        #endregion // IDataErrorInfo Members
    }
}
